package com.gla.giliair.web.admin;

import com.gla.giliair.entity.CabinInfo;
import com.gla.giliair.entity.Flight;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.BaseController;
import com.gla.giliair.web.vo.FlightVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 20:35
 */
@Api(tags = "航班管理模块")
@RestController
@RequestMapping("/api/adminX")
public class AdminFlightController extends BaseController {
    @ApiOperation(value = "获取全部航班列表", notes = "获取全部航班列表，有多种排序方式", produces = "application/json")
    @GetMapping("/findAllFlight")
    @ResponseBody
    public Map<String, Object> findAllFlight(//排序关键词：flightId、flightNumber、leavePoint、arrivePoint、leaveAirport、arriveAirport、
                                             // leaveTime、arriveTime、flightDuration、companyId、companyCode、companyName、
                                             // planemodelId、planemodelName、cabinInfoId、leastOriginalPrice、leastPrice、
                                             // createTime、updateTime、isDeleted
                                             //不填关键词默认按id逆向排序
                                             @RequestParam(value = "sort", defaultValue = "flightId") String sort,
                                             //正反序只对关键词生效
                                             @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                             @RequestParam(value = "showDeleted", defaultValue = "false") boolean showDeleted,
                                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize) {
        return flightService.findAllFlights(sort, isASC, showDeleted, page, pageSize);
    }

    @ApiOperation(value = "搜索航班列表", notes = "搜索航班列表，有多种排序方式", produces = "application/json")
    @PostMapping("/searchFlight")
    @ResponseBody
    public Map<String, Object> searchFlight(//排序关键词：flightId、flightNumber、leavePoint、arrivePoint、leaveAirport、arriveAirport、
                                            // leaveTime、arriveTime、flightDuration、companyId、companyCode、companyName、
                                            // planemodelId、planemodelName、cabinInfoId、leastOriginalPrice、leastPrice、
                                            // createTime、updateTime、isDeleted
                                            //不填关键词默认按id逆向排序
                                            @RequestParam(value = "sort", defaultValue = "flightId") String sort,
                                            //正反序只对关键词生效
                                            @RequestParam(value = "isASC", defaultValue = "false") boolean isASC,
                                            @RequestParam(value = "showDeleted", defaultValue = "false") boolean showDeleted,
                                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pageSize", defaultValue = "16") Integer pageSize,
                                            //准确搜索参数：出发日期(当日)(yyyy-mm-dd)；模糊搜索参数：航班号、出发地、到达地、出发机场、到达机场
                                            @RequestBody FlightVO searchParams) {
        return flightService.searchFlights(sort, isASC, showDeleted, page, pageSize, searchParams);
    }

    @ApiOperation(value = "添加航班信息", notes = "添加航班信息，自动计算部分字段，生成动态座位表", produces = "application/json")
    @PostMapping("/addFlight")
    @ResponseBody
    public Map<String, Object> addFlight(@RequestParam("companyId") Long companyId,
                                         @RequestParam("planemodelId") Long planemodelId,
                                         @RequestParam("discountId") Long discountId,
                                         //举例：934、5303、530X，只要后三/四位，具体规律由管理员按照指导手册人工决定（或采用国内早期老规律、或采用号段制新规律）
                                         @RequestParam("flightNumber") String flightNumber,
                                         @RequestParam("leavePoint") String leavePoint,
                                         @RequestParam("arrivePoint") String arrivePoint,
                                         @RequestParam("leaveAirport") String leaveAirport,
                                         @RequestParam("arriveAirport") String arriveAirport,
                                         //举例：2021-08-28 15:00:00
                                         @RequestParam("leaveTime") String leaveTime,
                                         //举例：2021-08-28 15:00:00
                                         @RequestParam("arriveTime") String arriveTime,
                                         @RequestParam("firstClassPrice") BigDecimal firstClassPrice,
                                         @RequestParam("businessClassPrice") BigDecimal businessClassPrice,
                                         @RequestParam("economyClassPrice") BigDecimal economyClassPrice) {
        if (companyId == null) {
            return MapGenerator.getRes(500, "请输入航空公司id！");
        }
        if (planemodelId == null) {
            return MapGenerator.getRes(501, "请输入飞机机型id！");
        }
        if (discountId == null) {
            return MapGenerator.getRes(502, "请输入折扣幅度id！");
        }
        if (!StringUtils.hasText(flightNumber)) {
            return MapGenerator.getRes(503, "请输入航班号非公司部分（谨慎决定）！");
        }
        if (!StringUtils.hasText(leavePoint)) {
            return MapGenerator.getRes(504, "请输入出发城市！");
        }
        if (!StringUtils.hasText(arrivePoint)) {
            return MapGenerator.getRes(505, "请输入到达城市！");
        }
        if (!StringUtils.hasText(leaveAirport)) {
            return MapGenerator.getRes(506, "请输入出发机场！");
        }
        if (!StringUtils.hasText(arriveAirport)) {
            return MapGenerator.getRes(507, "请输入到达机场！");
        }
        if (!StringUtils.hasText(leaveTime)) {
            return MapGenerator.getRes(508, "请输入出发时间！");
        }
        if (!StringUtils.hasText(arriveTime)) {
            return MapGenerator.getRes(509, "请输入到达时间！");
        }
        if (firstClassPrice == null) {
            return MapGenerator.getRes(510, "请输入头等舱价格！");
        }
        if (businessClassPrice == null) {
            return MapGenerator.getRes(511, "请输入商务舱价格！");
        }
        if (economyClassPrice == null) {
            return MapGenerator.getRes(512, "请输入经济舱价格！");
        }

        Flight flight = new Flight();
        flight.setCompanyId(companyId);
        flight.setPlanemodelId(planemodelId);
        flight.setFlightNumber(flightNumber);
        flight.setLeavePoint(leavePoint);
        flight.setArrivePoint(arrivePoint);
        flight.setLeaveAirport(leaveAirport);
        flight.setArriveAirport(arriveAirport);

        Date leaveDate = null;
        Date arriveDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            leaveDate = sdf.parse(leaveTime);
            arriveDate = sdf.parse(arriveTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return MapGenerator.getRes(513, "出发/到达时间格式不正确！");
        }
        flight.setLeaveTime(leaveDate);
        flight.setArriveTime(arriveDate);

        CabinInfo cabinInfo = new CabinInfo();
        cabinInfo.setDiscountId(discountId);
        cabinInfo.setFirstClassPrice(firstClassPrice);
        cabinInfo.setBusinessClassPrice(businessClassPrice);
        cabinInfo.setEconomyClassPrice(economyClassPrice);

        return flightService.addFlight(flight, cabinInfo);
    }

    @ApiOperation(value = "修改航班信息", notes = "修改航班信息，禁止修改飞机机型，限制修改公司", produces = "application/json")
    @PostMapping("/modifyFlight")
    @ResponseBody
    public Map<String, Object> modifyFlight(@RequestParam("flightId") Long flightId,
                                            @RequestParam(value = "companyId", required = false) Long companyId,
                                            @RequestParam(value = "discountId", required = false) Long discountId,
                                            //举例：934、5303、530X，只要后三/四位，具体规律由管理员按照指导手册人工决定（或采用国内早期老规律、或采用号段制新规律）
                                            @RequestParam(value = "flightNumber", required = false) String flightNumber,
                                            @RequestParam(value = "leavePoint", required = false) String leavePoint,
                                            @RequestParam(value = "arrivePoint", required = false) String arrivePoint,
                                            @RequestParam(value = "leaveAirport", required = false) String leaveAirport,
                                            @RequestParam(value = "arriveAirport", required = false) String arriveAirport,
                                            //举例：2021-08-28 15:00:00
                                            @RequestParam(value = "leaveTime", required = false) String leaveTime,
                                            //举例：2021-08-28 15:00:00
                                            @RequestParam(value = "arriveTime", required = false) String arriveTime,
                                            @RequestParam(value = "firstClassPrice", required = false) BigDecimal firstClassPrice,
                                            @RequestParam(value = "businessClassPrice", required = false) BigDecimal businessClassPrice,
                                            @RequestParam(value = "economyClassPrice", required = false) BigDecimal economyClassPrice) {
        if (flightId == null) {
            return MapGenerator.getRes(500, "请输入航班id！");
        }

        Flight flight = new Flight();
        flight.setFlightId(flightId);
        flight.setCompanyId(companyId);
        if (!flightNumber.equals("")) {
            flight.setFlightNumber(flightNumber);
        }
        if (!leavePoint.equals("")) {
            flight.setLeavePoint(leavePoint);
        }
        if (!arrivePoint.equals("")) {
            flight.setArrivePoint(arrivePoint);
        }
        if (!leaveAirport.equals("")) {
            flight.setLeaveAirport(leaveAirport);
        }
        if (!arriveAirport.equals("")) {
            flight.setArriveAirport(arriveAirport);
        }

        Date leaveDate = null;
        Date arriveDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (StringUtils.hasText(leaveTime)) {
                leaveDate = sdf.parse(leaveTime);
            }
            if (StringUtils.hasText(arriveTime)) {
                arriveDate = sdf.parse(arriveTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return MapGenerator.getRes(501, "出发/到达时间格式不正确！");
        }
        flight.setLeaveTime(leaveDate);
        flight.setArriveTime(arriveDate);

        CabinInfo cabinInfo = new CabinInfo();
        cabinInfo.setDiscountId(discountId);
        cabinInfo.setFirstClassPrice(firstClassPrice);
        cabinInfo.setBusinessClassPrice(businessClassPrice);
        cabinInfo.setEconomyClassPrice(economyClassPrice);

        return flightService.modifyFlight(flight, cabinInfo);
    }

    @ApiOperation(value = "删除航班信息", notes = "删除航班信息，可选择性删除全部相关的动态座位", produces = "application/json")
    @PostMapping("/deleteFlight")
    @ResponseBody
    public Map<String, Object> deleteFlight(@RequestParam("flightId") Long flightId,
                                            @RequestParam(value = "isSeatDeleted", defaultValue = "false") boolean isSeatDeleted) {
        if (flightId == null) {
            return MapGenerator.getRes(500, "请输入航班id！");
        }
        return flightService.deleteFlight(flightId, isSeatDeleted);
    }
}
